package za.org.jcicada.genss.impl;

import java.net.URI;
import java.net.URISyntaxException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.BasicConfigurator;

import za.org.jcicada.genss.IGenSS;
import za.org.jcicada.genss.ServiceURN;
import za.org.jcicada.genss.SystemURN;
import za.org.jcicada.genss.alarm.impl.AlarmSubject;
import za.org.jcicada.genss.lifecycle.impl.LifeCycleServer;
import za.org.jcicada.genss.sensor.SensorURN;
import za.org.jcicada.genss.sensor.impl.SensorSubject;

public class MockGenSS extends GenSS {
    
    public ServiceURN serviceURN;
    public URI serviceLocationURI;

    public MockGenSS(SystemURN systemURN) throws URISyntaxException {
        super(systemURN);
    }

    public MockGenSS(SystemURN systemURN, SensorSubject sensorSubject, AlarmSubject alarmSubject, LifeCycleServer lifeCycleServer) throws URISyntaxException {
        super(systemURN, sensorSubject, alarmSubject, lifeCycleServer);
    }
    
    @Override
    protected void registerService(ServiceURN serviceURN, URI serviceLocationURI) {
        this.serviceURN = serviceURN;
        this.serviceLocationURI = serviceLocationURI;
    }

    @Override
    protected void deregisterService(ServiceURN serviceURN, URI serviceLocationURI) {
        this.serviceURN = serviceURN;
        this.serviceLocationURI = serviceLocationURI;
    }

    @Override
    protected void duringInitialize() throws Exception {
    	super.duringInitialize();
        setupAcceptors("inprocess://unused", "inprocess://unused", "inprocess://unused");
        setMode(ModeEnum.MANAGE);
    }
    
    @Override
    protected void duringShutdown() throws Exception {
        // finally inform that the shutdown has completed
        setMode(ModeEnum.SHUTDOWN_COMPLETE);
        super.duringShutdown();
    }
    
    @Override
    public boolean removeSensor(SensorURN sensorURN) throws URISyntaxException {
        return super.removeSensor(sensorURN);
    }
    
    public ModeEnum getMode() {
        if (modeSensor == null) {
            return null;
        }
        if (modeSensor.getValue() == null) {
            return null;
        }
        if (modeSensor.getValue().getValue() == null) {
            return null;
        }
    	return ModeEnum.valueOf(modeSensor.getValue().getValue().toString());
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /**
         * Logger object used for logging messages.
         */
        Log logger = LogFactory.getLog(MockGenSS.class);

        BasicConfigurator.configure();
        try {
            SystemURN systemURN = new SystemURN(args[0]);
            boolean restart = true;
            while (restart) {
                IGenSS genss = new MockGenSS(systemURN);
                genss.initialize();
                restart = genss.waitHalt();
                genss.shutdown();
            }
        } catch (URISyntaxException ex) {
            logger.fatal(String.format("Unable to start GenSS with invalid URN: %s", args[0]), ex);
        }
    }

}
